package com.tust.service;

import com.tust.bean.Teacher;
import com.tust.dao.TeacherDao;

import java.sql.Connection;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class TeacherServiceImpl implements BaseService{

    // 教师表业务逻辑
    public void service(Connection connection) {

        Scanner scanner = new Scanner(System.in);
        String type;

        List<Teacher> list;

        while (true) {
            System.out.println("请输入教师管理操作命令：add->添加，edit->编辑,del->删除,Query->查询，QUIT->退出");
            TeacherDao teacherDao = new TeacherDao(connection);
            type = scanner.next();

            if (type.equalsIgnoreCase("add")) {
                System.out.println("请依次单独输入教师工号、姓名、年龄、职业：");
                Teacher teacher = new Teacher();
                teacher.setTid(scanner.nextInt());
                teacher.setTname(scanner.next());
                teacher.setTage(scanner.nextInt());
                teacher.setTprofession(scanner.next());
                if (teacherDao.insert(teacher)) {
                    System.out.println("添加成功");
                    list = teacherDao.query();
                    print(list);
                } else {
                    System.out.println("添加失败");
                }
            } else if (type.equalsIgnoreCase("del")) {
                list = teacherDao.query();
                print(list);
                System.out.println("删除指定教师请输入教师工号，删除所有教师输入all。");
                type = scanner.next();
                if (type.equalsIgnoreCase("all")) {
                    if (teacherDao.delete()) {
                        System.out.println("所有教师记录删除成功！");
                    } else {
                        System.out.println("所有教师记录删除失败！");
                    }
                } else {
                    if (teacherDao.delete(Integer.parseInt(type))) {
                        System.out.println(type + "教师删除成功！");
                        list = teacherDao.query();
                        print(list);
                    } else {
                        System.out.println(type + "教师删除失败！");
                    }
                }
            } else if (type.equalsIgnoreCase("edit")) {
                // 打印教师信息表
                list = teacherDao.query();
                print(list);

                System.out.println("请依次单独输入要更新教师的教师工号、姓名、年龄、职业：");
                Teacher teacher = new Teacher();
                teacher.setTid(scanner.nextInt());
                teacher.setTname(scanner.next());
                teacher.setTage(scanner.nextInt());
                teacher.setTprofession(scanner.next());
                if (teacherDao.update(teacher)) {
                    System.out.println("更新成功");
                    list = teacherDao.query();
                    print(list);
                } else {
                    System.out.println("更新失败");
                }
            } else if (type.equalsIgnoreCase("query")) {
                System.out.println("查询指定教师请输入教师工号，查询所有教师输入all。");
                type = scanner.next();
                if (type.equalsIgnoreCase("all")) {
                    list = teacherDao.query();
                    print(list);

                } else {
                    Teacher teacher = teacherDao.query(Integer.parseInt(type));
                    if (teacher != null) {
                        System.out.println(teacher.getTid() + "\t" + teacher.getTname() + "\t" + teacher.getTage() + "\t" +teacher.getTprofession());
                    } else {
                        System.out.println("查询的工号为:" + type + "的教师不存在！");
                    }
                }
            } else if (type.equalsIgnoreCase("QUIT")) {
                teacherDao.close();
                break;
            }
        }
    }

    @Override
    public void print(List list) {

        if(list.size()>0) {
            Iterator<Teacher> iterator = list.iterator();
            System.out.println("教师工号\t姓名\t\t年龄\t职业");
            while (iterator.hasNext()) {
                Teacher teacher = iterator.next();
                String tid = String.format("%-8s", teacher.getTid());
                String tname = String.format("%-6s", teacher.getTname());
                String tage = String.format("%-4s", teacher.getTage());
                String tprofession = String.format("%-6s", teacher.getTprofession());
                System.out.println(tid + tname + tage + tprofession);
            }
        }else{
            System.out.println("无教师记录，请录入！");
        }

    }


}
